package com.wang.myapplication.fragment;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.wang.myapplication.R;
import com.wang.myapplication.base.BaseFragment;
import com.wang.myapplication.base.MyBaseAdapter;
import com.wang.myapplication.network.bean.WildConfigResponse;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * create an instance of this fragment.
 */
public class FirstFragment extends BaseFragment {
    @BindView(R.id.gViewFrist)
    GridView gViewFrist;

    @Override
    public int bindLayout() {
        return R.layout.fragment_first;
    }

    @Override
    public void initView(View view) {
        Banner banner = view.findViewById(R.id.banner);
        List<String> imageUrls = new ArrayList<>();
        List<WildConfigResponse.Loopimage> loopimages = WildConfigResponse.GetLoopimage();
        imageUrls.add(loopimages.get(1).getNormalimage());
        imageUrls.add(loopimages.get(2).getNormalimage());
        imageUrls.add(loopimages.get(3).getNormalimage());
        imageUrls.add(loopimages.get(4).getNormalimage());
        banner.setImages(imageUrls).setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                /**
                 注意：
                 1.图片加载器由自己选择，这里不限制，只是提供几种使用方法
                 2.返回的图片路径为Object类型，由于不能确定你到底使用的那种图片加载器，
                 传输的到的是什么格式，那么这种就使用Object接收和返回，你只需要强转成你传输的类型就行，
                 切记不要胡乱强转！
                 */

                ////Glide 加载图片简单用法
                Glide.with(context).load(path).into(imageView);
                //
                ////Picasso 加载图片简单用法
                //Picasso.with(context).load(path).into(imageView);

                //用fresco加载图片简单用法，记得要写下面的createImageView方法
                //Uri uri = Uri.parse((String) path);
                //imageView.setImageURI(uri);
            }

            //提供createImageView 方法，如果不用可以不重写这个方法，主要是方便自定义ImageView的创建
            //@Override
            //public ImageView createImageView(Context context) {
            //    return super.createImageView(context);
            //
            //}
        }).start();


        List<FirstFragmentContent> gViewLists = new ArrayList<>();
        gViewLists.add(new FirstFragmentContent(R.mipmap.icon1, "我的订阅"));
        gViewLists.add(new FirstFragmentContent(R.mipmap.icon2, "知识排行"));
        gViewLists.add(new FirstFragmentContent(R.mipmap.icon3, "行业动态"));
        gViewLists.add(new FirstFragmentContent(R.mipmap.icon4, "作业指引"));
        gViewLists.add(new FirstFragmentContent(R.mipmap.icon5, "最新归档"));
        gViewLists.add(new FirstFragmentContent(R.mipmap.icon6, "知识统计"));


        gViewFrist.setAdapter(new MyBaseAdapter<FirstFragmentContent>(gViewLists, R.layout.gviewitem_firstlist) {
            @Override
            public void handItemView(ViewHolder holder, FirstFragmentContent pFirstFragmentContent) {
                ImageView imgIcon = holder.findViewById(R.id.imgIcon);
                Glide.with(mContext).load(pFirstFragmentContent.getIconPath()).into(imgIcon);

                TextView textView = holder.findViewById(R.id.tvDesc);
                textView.setText(pFirstFragmentContent.getDescrip());
            }
        });

        gViewFrist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(mContext, "id是"+id   +"positon是"+position, Toast.LENGTH_SHORT).show();
                switch (position) {
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    case 4:
                        break;
                    case 5:
                        break;
                }
            }
        });


    }

    @Override
    public void doBusiness(Context mContext) {

    }

    @Override
    public void widgetClick(View v) {

    }

    private class FirstFragmentContent {
        int iconPath;
        String descrip;

        public FirstFragmentContent(int pIconPath, String pDescrip) {
            iconPath = pIconPath;
            descrip = pDescrip;
        }

        public int getIconPath() {
            return iconPath;
        }

        public void setIconPath(int pIconPath) {
            iconPath = pIconPath;
        }

        public String getDescrip() {
            return descrip;
        }

        public void setDescrip(String pDescrip) {
            descrip = pDescrip;
        }
    }
}
